﻿using System;
using System.Web;
using ElectronicCommerce.Framework.Web.Configuration;
using ElectronicCommerce.Framework.Web.Configuration.Resource;

namespace ElectronicCommerce.Framework.Web.ResourceManagement
{
    public class ResourceBuilder
    {

        public virtual string ThemeFolder
        {
            get { return "Themes"; }
        }

        /// <summary>
        /// Build CSS Path
        /// </summary>
        /// <param name="cssName">the css file name</param>
        /// <returns></returns>
        public string BuildCss(string cssName)
        {
            string cssSite = ECContext.IsSecureConnection ? ConfigHelper.ResourceSiteConfig.CssHttpsSite : ConfigHelper.ResourceSiteConfig.CssHttpSite;
            //string themeName = string.IsNullOrEmpty(ConfigHelper.ThemeConfig.DefaultThemeName) ? ECContext.Current.Theme : ConfigHelper.ThemeConfig.DefaultThemeName;

            return string.Format("{0}/{1}", cssSite, cssName).ToLower();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cssAlias"></param>
        /// <returns></returns>
        public string BuildCss(Enum cssAlias)
        {
            Css css = ConfigHelper.CssConfig.GetCss(cssAlias.ToString());
            return BuildCss(css.FileName);
        }

        /// <summary>
        /// Build Js Path
        /// </summary>
        /// <param name="cssName">the Js file name</param>
        /// <returns></returns>
        public string BuildJs(string jsName)
        {
            string jsSite = ECContext.IsSecureConnection ? ConfigHelper.ResourceSiteConfig.JsHttpsSite : ConfigHelper.ResourceSiteConfig.JsHttpSite;

            if (jsName.StartsWith("~/"))
            {
                string query = string.Empty;
                if (jsName.Contains("?"))
                {
                    query = jsName.Substring(jsName.IndexOf('?'));
                    jsName = jsName.Substring(0, jsName.IndexOf('?'));
                }
                return VirtualPathUtility.ToAbsolute(jsName) + query;
            }

            string scriptFolder = "Scripts";

            return string.Format("{0}/{1}/{2}", jsSite.TrimEnd('\\', '/'), scriptFolder, jsName).ToLower();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="jsAlias"></param>
        /// <returns></returns>
        public string BuildJs(Enum jsAlias)
        {
            Javascript js = ConfigHelper.JavascriptConfig.GetJavascript(jsAlias.ToString());

            if (js.UED)
            {
                return BuildUEDJs(js.FileName);
            }
            else
            {
                return BuildJs(js.FileName);
            }
        }

        public string BuildUEDJs(string jsName)
        {
            string jsSite = ECContext.IsSecureConnection ? ConfigHelper.ResourceSiteConfig.JsUEDHttpsSite : ConfigHelper.ResourceSiteConfig.JsUEDHttpSite;

            return string.Format("{0}/{1}", jsSite.TrimEnd('\\', '/'), jsName).ToLower();
        }

        /// <summary>
        /// Build Image Path
        /// </summary>
        /// <param name="imageName"></param>
        /// <returns></returns>
        public string BuildMedia(string mediaName)
        {
            string imageSite = ECContext.IsSecureConnection ?
                ConfigHelper.ResourceSiteConfig.ImageHttpsSite : ConfigHelper.ResourceSiteConfig.ImageHttpSite;

            return string.Format("{0}/{1}", imageSite, mediaName).ToLower();
        }
    }
}
